window.onload = getCountry();


function getCountry() {
	var request = '/WSI/country';
	var result = $.get(request).done(function(data) {
                fillCountry(data)
        }).fail(function() {
                console.log("Unable to perform request : " + request);
        })

}

function fillCountry(data) {
	
	var countires = data.split(",");
	console.log(countires);

	var co = document.getElementById('country');
	for(var i = 0; i < countires.length; i++) {	
		var o = new Option;		
		o.text = countires[i];
		o.value = countires[i];
		co.options[i] = o;
	}

	var x=document.getElementById("country").selectedIndex;
	var y=document.getElementById("country").options;
	
	co.onchange=function(){ 
 		var chosenoption=this.options[this.selectedIndex] ;
 		getCity(chosenoption.value);
	}
	
}



function getCity(country) {
	var request = '/WSI/city?country=' + country;
	var result = $.get(request).done(function(data) {
        	fillCity(data);
        }).fail(function() {
        	console.log("Unable to perform request : " + request);
        })

}

function removeOptions(selectbox)
{
    var i;
    for(i=selectbox.options.length-1;i>=0;i--)
    {
        selectbox.remove(i);
    }
}


function fillCity(data) {
	var cities = data.split(",");
	var co = document.getElementById('city');
	
	removeOptions(co);

	

	for(var i = 0; i < cities.length; i++) {	
		var o = new Option;		
		o.text = cities[i];
		o.value = cities[i];
		co.options[i] = o;
	}

}


